<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use \think\Loader;
use think\Request;

/*
** 刮刮乐 控制器
*/

class Scratch extends Adminbase {
    private static $_scratch = null; 	// 数据表对象

    protected $params;
    public $request;
    // 优先加载
    public function _initialize() {
        $this->request = Request::instance();
        $this->request->filter(['strip_tags','htmlspecialchars']);
        $this->params =  $this->request->param();
        parent::_initialize();
        self::$_scratch = Loader::model('Scratch');
    }

    /**
     * 刮刮乐的转盘数据总览
     * @param page
     * @param page_size
     * @param start_date,end_date,status,created 这几个字段是搜索字段
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function index() {
        $params = $this->params;

        $nowPage = 1;
        if(isset($params['page']) && $params['page']){
            $nowPage = intval($params['page']);
            $nowPage = $nowPage > 0 ? $nowPage : 1;
        }

        $pageSize = 10;

        $data = self::$_scratch->get_list_for_bk($nowPage,$pageSize,$params);

        $status = 2;
        if (isset($params['status'])) {
            if ($params['status'] == 0 || $params['status'] == 1) {
                $status = $params['status'];
            }
        }
        $data = array(
            'data' 		    => $data['data'],
            'allpages' 		=> $data['allpages'], 	// 总页数
            'nowpage' 		=> $data['nowpage'], 	// 当前页
            'shop_id'       => $params['shop_id'],
            'scratch_id'    => $params['scratch_id'],
            'status'        => $status,
            'start_date'    => $params['start_date'],
            'end_date'      => $params['end_date'],
            'startime'      => $params['startime'],
            'endtime'       => $params['endtime'],
        );
        $this -> assign($data);
        return view('index');
    }

    /**
     * 修改刮刮乐状态
     * @param status
     * @param scratch_id
     */
    public function edit_status() {
        $params = $this->params;
        $status = $params['status'];
        $scratchId = $params['scratch_id'];

        $data = self::$_scratch->edit_status($status,$scratchId);
        if ($data) {
            $this -> success('操作成功');
        }
        $this -> success('操作失败');
    }

    /**
     * 修改刮刮乐奖品状态
     * @param status
     * @param id 奖品id
     */
    public function edit_prize_status() {
        $params = $this->params;
        $status = $params['status'];
        $prizeId = $params['id'];

        $data = self::$_scratch->edit_prize_status($status,$prizeId);
        if ($data) {
            $this -> success('操作成功');
        }
        $this -> error('操作失败');
    }

    /**
     * 更新奖品数据
     * @param id 奖品id
     * @return \think\response\Json
     */
    public function update_info() {
        $params = $this->params;
        if (empty($params['id'])) {
            $this -> error('没有奖品id');
        }
        $prizeId = $params['id'];
        unset($params['id']);
        $params['created'] = strtotime($params['created']);
        $res = self::$_scratch->update_game_info($params,$prizeId);
        if ($res) {
            $this -> success('操作成功');
        } else {
            $this -> error('操作失败');
        }
    }

    /**
     * 奖品列表。包含每个奖品对应的中奖人数
     * @param scratch_id
     */
    public function prize_list() {
        $params = $this->params;
        if (empty($params['scratch_id'])) {
            $this -> error('没有刮刮乐id');
        }
        $turntableId = $params['scratch_id'];
        $data = self::$_scratch->prize_list_for_bk($turntableId);

        $this -> assign(['data' => $data]);
        return view('list');

    }

    /**
     * 中奖列表。每个奖品中的奖品
     * @param prize_id 奖品id
     * @param turntable_id
     */
    public function win_list() {
        $params = $this->params;

        if (empty($params['prize_id'])) {
            $this -> error('没有奖品id');
        }
        if (empty($params['scratch_id'])) {
            $this -> error('没有刮刮乐id');
        }
        // 当前页
        $nowPage = 1;
        if(isset($params['page']) && $params['page']){
            $nowPage = intval($params['page']);
            $nowPage = $nowPage > 0 ? $nowPage : 1;
        }

        $pageSize = 20;

        $data = self::$_scratch->win_list_for_bk($params['prize_id'],$params['scratch_id'],$nowPage,$pageSize);

        $data = array(
            'data' 		    => $data['data'],
            'allpages' 		=> $data['allpages'], 	// 总页数
            'nowpage' 		=> $data['nowpage'], 	// 当前页
        );
        $this -> assign($data);
        return view('win_list');
    }

    /**
     * 刮刮乐奖品编辑页面数据，获取单个奖品数据
     * @param prize_id
     * @return \think\response\View
     */
    public function edit_page() {
        $params = $this->params;

        if (empty($params['prize_id'])) {
            $this -> error('操作失败');
        }

        $data = self::$_scratch->edit_info($params['prize_id']);

        $this -> assign(['lists' => $data]);
        return view('edit');
    }

}
